<?php

class EditorHelper extends AppHelper {
	var $campos = '';
	var $modo = '';
	var $tema = '';
	var $piel = '';
	var $plugins = '';
	var $pluginsBase = 'safari,spellchecker,pagebreak,style,layer,table,save,advhr,advimage,advlink,emotions,iespell,inlinepopups,insertdatetime,preview,media,searchreplace,print,contextmenu,paste,directionality,fullscreen,noneditable,visualchars,nonbreaking,xhtmlxtras,template';
	var $barras = array();
	var $barrasBase = array(
			'theme_advanced_buttons1' => 'newdocument,|,bold,italic,underline,strikethrough,|,justifyleft,justifycenter,justifyright,justifyfull,|,styleselect,formatselect,fontselect,fontsizeselect',
	        'theme_advanced_buttons2' => 'cut,copy,paste,pastetext,pasteword,|,search,replace,|,bullist,numlist,|,outdent,indent,blockquote,|,undo,redo,|,link,unlink,anchor,image,cleanup,help,code,|,insertdate,inserttime,preview,|,forecolor,backcolor',
	        'theme_advanced_buttons3' => 'tablecontrols,|,hr,removeformat,visualaid,|,sub,sup,|,charmap,emotions,iespell,media,advhr,|,print,|,ltr,rtl,|,fullscreen',
	        'theme_advanced_buttons4' => 'insertlayer,moveforward,movebackward,absolute,|,styleprops,spellchecker,|,cite,abbr,acronym,del,ins,attribs,|,visualchars,nonbreaking,template,blockquote,pagebreak,|,insertfile,insertimage'
		);
	var $formatos = array();
	var $formatosBase = array(
			1 => array('title' => 'Bold text', 'inline' => 'b'),
			2 => array('title' => 'Red text', 'inline' => 'span', 'styles' => array('color' => '#ff0000')),
			3 => array('title' => 'Red Header', 'inline' => 'h1', 'styles' => array('color' => '#ff0000')),
			4 => array('title' => 'Example 1', 'inline' => 'span', 'classes' => 'example'),
			5 => array('title' => 'Table styles'),
			6 => array('title' => 'Table row 1', 'selector' => 'tr', 'classes' => 'tablerow1')
		);

	public function configuracion($modo = "e", $nombres = array('default'), $opciones = array('tema' => 'advanced', 'piel' => 'o2k7')) {
		$this->tema = $opciones['tema'];
		$this->piel = $opciones['piel'];

		switch ($modo) {
			case 'e':
			default:
				$this->modo = 'exact';
			break;
			case 't':
				$this->modo = 'textareas';
			break;
			case 'st':
				$this->modo = 'specific_textareas';
			break;
		}

		if($modo != 't'){
			$cant = count($nombres);
			$i = 0;

			foreach ($nombres as $valor){
				switch ($modo) {
					case 'e':
						$this->campos .= "$valor,";
					break;
					case 'st':
						if($cant == 1)
							$this->campos = "$valor";
						else{
							if($i == 0)
								$this->campos = "/($valor|";
							else
								$this->campos .= "$valor|";
							$i++;
						}
					break;
				}
			}

			switch ($modo) {
				case 'e':
					$this->campos = preg_replace("/,$/", "", $this->campos);
				break;
				case 'st':
					$this->campos = preg_replace("/|$/", "", $this->campos);
					$this->campos .= ")/";
				break;
			}
		}else
			$this->campos = '';
	}

	public function confPlugins($nombres = null, $combinar = 0){
		$this->plugins = (!empty($nombres)) ? (!$combinar) ? $nombres : $this->pluginsBase . ",$nombres" : $this->pluginsBase;
	}

	public function confBarras($nombres = null, $combinar = 0){
		if(!empty($nombres)){
			$i = 1;

			if($combinar){
				$i = 5;
				$this->barras = $this->barrasBase;
			}

			foreach ($nombres as $valor) {
				$this->barras["theme_advanced_buttons$i"] = "$valor";
				$i++;
			}
		}else
			$this->barras = $this->barrasBase;
	}

	public function confFormatos($nombres = null, $combinar = 0){
		if(!empty($nombres)){
			$i = 1;
	
			if($combinar){
				$i = 7;
				$this->formatos = $this->formatosBase;
			}
	
			foreach ($nombres as $valor) {
				foreach ($valor as $indice2 => $valor2){
					if(!is_array($valor2))
						$this->formatos[$i][strtolower($indice2)] = '\"'.$valor2.'\"';
					else
						foreach ($valor2 as $indice3 => $valor3)
							$this->formatos[$i][strtolower($indice2)][strtolower($indice3)] = '\"'.$valor3.'\"';
				}
				$i++;
			}
		}else
			$this->formatos = $this->formatosBase;
	}

	public function armarEditor(){
		if(!isset($this->plugins) || empty($this->plugins))
			$this->confPlugins();
		if(!isset($this->barras) || empty($this->barras))
			$this->confBarras();
		if(!isset($this->formatos) || empty($this->formatos))
			$this->confFormatos();
	}

	public function mostrarEditor(){
		$this->armarEditor();
		
		$resultado = '';
		$resultado .= "\n<script type='text/javascript'>";
		$resultado .= "tinyMCE.init({
        // Opciones Generales
        mode : '{$this->modo}',
        elements : '{$this->campos}',
        theme : '{$this->tema}',
        skin : '{$this->piel}',
        plugins : '{$this->plugins}',";
        $resultado .= "// Opciones del tema\n";
        
        foreach ($this->barras as $indice => $valor) {
        	$resultado .= "$indice: '$valor',\n";
        }
        
        $resultado .= "
        theme_advanced_toolbar_location : 'top',
        theme_advanced_toolbar_align : 'left',
        theme_advanced_statusbar_location : 'bottom',
        theme_advanced_resizing : true,

        // Ejemplo de contenido css (debe ser el CSS de su aplicacion)
        //content_css : 'css/example.css',

        // Lista desplegable para enlaces/imagenes/media/plantillas dialogos
        template_external_list_url : 'js/template_list.js',
        external_link_list_url : 'js/link_list.js',
        external_image_list_url : 'js/image_list.js',
        media_external_list_url : 'js/media_list.js',

		// Ejemplos para aplicar formatos predefinidos
		style_formats: [\n";
		
		foreach ($this->formatos as $valor2) {
			$resultado .= "{";
			foreach ($valor2 as $indice3 => $valor3) {
				if(!is_array($valor3))
					$resultado .= "$indice3: '$valor3',";
				else{
					$resultado .= "$indice3: {";
					foreach ($valor3 as $indice4 => $valor4)
						$resultado .= "$indice4: '$valor4',";
					$resultado = preg_replace("/,$/", "", $resultado);
					$resultado .= "},";
				}
			}
			$resultado = preg_replace("/,$/", "", $resultado);
			$resultado .= "},";
        }
        $resultado = preg_replace("/,$/", "", $resultado);

        $resultado .= "]
	});
</script>";

		echo $resultado;
	}
}
